package com.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.module.commonui.R;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.util.TemperatureHumidityUtil;

public class TemperatureHumidityView extends LinearLayout {
    private static final String DEFAULT_TEMPERATURE_VAL = "10°C";
    private static final String DEFAULT_HUMIDITY_VAL = "10%";
    private RelativeLayout mRelativeLayout;
    private TextView mContent;
    private TextView mTemperatureHumidity;
    @SuppressLint("UseSwitchCompatOrMaterialCode")
    public Switch mSwitch;
    public boolean mIsCentigrade = true;
    public boolean mIsSwitch = true;
    private String mStrTemperatureHumidity;
    private boolean mIsTemperature = true;

    public int getDegrees() {
        return mDegrees;
    }

    private int mDegrees = 0;
    public String getStrTemperatureHumidity() {
        return mStrTemperatureHumidity;
    }

    public TemperatureHumidityView(Context context) {
        this(context, null);
    }

    public TemperatureHumidityView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TemperatureHumidityView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context, attrs);
    }

    private void initViews(Context context, @Nullable AttributeSet attrs) {
        View view = View.inflate(context, R.layout.temperature_humidity_layout, this);
        mRelativeLayout = view.findViewById(R.id.rl_temperature_humidity);
        mContent = view.findViewById(R.id.tv_content);
        mTemperatureHumidity =  view.findViewById(R.id.tv_temperature_humidity);
        mSwitch = view.findViewById(R.id.sw_settings_content);

        @SuppressLint("CustomViewStyleable") TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TemperatureHumidityView);
        String text = a.getString(R.styleable.TemperatureHumidityView_itemContent);
        mIsCentigrade = a.getBoolean(R.styleable.TemperatureHumidityView_isCentigrade, true);
        mIsTemperature = a.getBoolean(R.styleable.TemperatureHumidityView_isTemperature, true);
        boolean isSwitch = a.getBoolean(R.styleable.TemperatureHumidityView_isSwitch, true);
        mContent.setText(text);

        setSwitch(isSwitch);
        a.recycle();
    }

    public void setSwitch(boolean isSwitch){
        mIsSwitch = isSwitch;
        mSwitch.setChecked(isSwitch);
        if (mIsSwitch){
            mRelativeLayout.setBackgroundResource(R.drawable.item_temperature_humidity_bg);
            mTemperatureHumidity.setTextColor(ResourcesUtil.getResources().getColor(R.color.colorDefaultThemeGreen));
            if (!StringUtils.isEmpty(mStrTemperatureHumidity)){
                mTemperatureHumidity.setText(mStrTemperatureHumidity);
            }
        }else{
            mRelativeLayout.setBackgroundResource(R.drawable.item_temperature_humidity_disable_bg);
            mTemperatureHumidity.setTextColor(ResourcesUtil.getResources().getColor(R.color.device_add_type_description_color));
            mTemperatureHumidity.setText(ResourcesUtil.getString(R.string.settings_pir_sensitivity_filter_off));
        }
    }

    public void setRelativeLayoutClickListener(@Nullable OnClickListener l) {
        mRelativeLayout.setOnClickListener(l);
    }
    public void setOnClickListener(@Nullable OnClickListener l) {
        mSwitch.setOnClickListener(l);
    }
    public void setOnCheckedChangeListener(@Nullable CompoundButton.OnCheckedChangeListener listener){
        mSwitch.setOnCheckedChangeListener(listener);
    }

    public void setContent(String content){
        mContent.setText(content);
    }

    public void setCentigrade(boolean centigrade) {
        if (mIsCentigrade != centigrade){
            mIsCentigrade = centigrade;
            if (mIsTemperature){
                if (mIsCentigrade){
                    mDegrees = (int)TemperatureHumidityUtil.fahrenheitToCelsius(mDegrees);
                }else{
                    mDegrees = (int)TemperatureHumidityUtil.celsiusToFahrenheit(mDegrees);
                }
            }
            setTemperatureHumidity(mDegrees);
        }
    }

    public void setContent(@StringRes int resId){
        String name = ResourcesUtil.getString(resId);
        mContent.setText(name);
    }

    public void setTemperatureHumidity(int temperatureHumidity){
        mDegrees = temperatureHumidity;
        String title = temperatureHumidity + "%";
        if (mIsTemperature){
            title = String.format(ResourcesUtil.getString(R.string.txt_centigrade), temperatureHumidity);
            if (!mIsCentigrade){
                title = String.format(ResourcesUtil.getString(R.string.txt_fahrenheit_degree), temperatureHumidity);
            }
        }
        mStrTemperatureHumidity = title;
        if (mIsSwitch){
            mTemperatureHumidity.setText(title);
        }else{
            mTemperatureHumidity.setText(ResourcesUtil.getString(R.string.settings_pir_sensitivity_filter_off));
        }
    }

    public TextView getContent() {
        return mContent;
    }

    public void setTemperatureHumidity(String temperatureHumidity){
        if (!isChecked()){
            mTemperatureHumidity.setText(ResourcesUtil.getString(R.string.settings_pir_sensitivity_filter_off));
        }else{
            mStrTemperatureHumidity = temperatureHumidity;
            if (StringUtils.isEmpty(mStrTemperatureHumidity)){
                mStrTemperatureHumidity = mIsTemperature ? DEFAULT_TEMPERATURE_VAL : DEFAULT_HUMIDITY_VAL;
            }
            mTemperatureHumidity.setText(mStrTemperatureHumidity);
        }
    }

    public void setChecked(boolean checked) {
        mSwitch.setChecked(checked);
    }

    public boolean isChecked() {
        return mSwitch.isChecked();
    }
}